/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import agh.rozproszony.model.User;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Marcin_PC
 */
public class Table extends AbstractTableModel {

    private List<UserRowWrapper> rows;
    private final static Object[] columnNames = {"Login", "e-mail", "Usun"};
    private final static int LOGIN_IDX = 0;
    private final static int EMAIL_IDX = 1;
    private final static int REMOVE_IDX = 2;

    public Table() {
        this.rows = new ArrayList<UserRowWrapper>();
    }

    public List<UserRowWrapper> getRows()
    {
        return rows;
    }
    
    @Override
    public int getRowCount() {
        if (rows == null) {
            return 0;
        }
        return rows.size();
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        if (rows == null) {
            return null;
        }
        UserRowWrapper row = (UserRowWrapper) rows.get(rowIndex);
        switch (columnIndex) {
            case LOGIN_IDX:
                return row.getLogin();
            case EMAIL_IDX:
                return row.getEmail();
            case REMOVE_IDX:
                return row.isRomove();
            default:
                return row;
        }
    }

    @Override
    public String getColumnName(int column) {
        return columnNames[column].toString();
    }

    public void setModelData(List<User> users) {
        rows.clear();
        for (User user : users) {
            UserRowWrapper row = null;
            row.setLogin(user.getLogin());
            row.setEmail(user.getEmail());
            row.setRomove(false);

            rows.add(row);
        }
    }

    public UserRowWrapper getRow(int row) {
        return rows.get(row);
    }

    public void addRow(User user) {
        
        if(validate(user)==false)return;
        
        UserRowWrapper row = new UserRowWrapper();
        row.setLogin(user.getLogin());
        row.setEmail(user.getEmail());
        row.setRomove(Boolean.FALSE);

        rows.add(row);
    }
    
    public void removeSelectedItems()
    {
      Collection toRemove = new LinkedList();
      for(UserRowWrapper row : rows)
      {
          if(row.isRomove()==true) toRemove.add(row);
      }
      rows.removeAll(toRemove);
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {

        switch (columnIndex) {
            case LOGIN_IDX:
                return String.class;
            case EMAIL_IDX:
                return String.class;
            case REMOVE_IDX:
                return Boolean.class;
            default:
                return Boolean.class;
        }
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        switch (columnIndex) {
            case REMOVE_IDX:
            {
                rows.get(rowIndex).setRomove(!rows.get(rowIndex).isRomove());
                return true;
            }
            default:
                return false;
        }
    }
    
    private boolean validate(User user)
    {
        for( UserRowWrapper row : rows )
        {
            if(row.getEmail().equals(user.getEmail()))
            {
                return false;
            }
        }
        return true;
    }
    
    
}
